var path = $('body').attr('ctxPath');
$(function() {
	$('.list-content-div table thead tr th:last').attr("width","13%");
});

// 新增按钮
$("#add").on('click',function() {
	$("#addModal :input").val("");// 清空表单
	$('#addModal .modal-title').text("新增用户");//更改标题
	$('.modal-footer #update').hide();
	$('.modal-footer #save').show();
	$('#loginNo').attr('readonly',false);
	//获取当前登录账号所属公司
	$.post(path +'/userManage/getCompanyInfo?_t='+new Date().getTime(),function(data){
		$('#companyName').val(data.companyName);
		$('#companyId').val(data.companyId);
		$('#companyCode').val(data.companyCode);
		
		$('#addModal').modal({show : true,moveable : true});
	});

});

//新增保存按钮
$('#save').on('click',function(){
	
	var params = $('#userForm').serialize();
	var url = path + '/userManage/addUser?_t='+new Date().getTime();
	 $.post(url,params,function(data){
		 if(data.flag){
			 $('#addModal').modal("hide");
			 layer.alert(data.msg);
			 listui.refresh();
		 }else{
			 layer.msg(data.msg);
		 }
	 });
});



//编辑
$('.list-content-div table>tbody').delegate('tr>td>.edit-item', 'click', function() {
	//$('#addModal :input').val('');//清空表单
	$('#addModal .modal-title').text("编辑用户");
	//根据id 查询信息
	var id = $(this).attr('data-id');
	var url = path+'/userManage/showItem/'+id+'?_t='+new Date().getTime();
	$.post(url,function(data){
		$('#id').val(data.id);
		$('#companyId').val(data.companyId);
		$('#companyCode').val(data.companyCode);
		$('#companyName').val(data.companyName);
		$('#loginNo').val(data.loginNo);
		$('#loginNo').attr('readonly',true);
		$('#_nick').val(data.nick);
		$('#role').val(data.role);
	});
	
	$('.modal-footer #save').hide();
	$('.modal-footer #update').show();
	$('#addModal').modal({'show':true,'moveable':true});
	
});

//更新按钮
$('#update').on('click',function(){
	var params = $('#userForm').serialize();
	var url = path + '/userManage/editUserInfo?_t='+new Date().getTime();
/*	if($('#role').val()==null){
		layer.msg('角色不能为空!')
		return;
	}*/
	$.post(url,params,function(data){
		 if(data.flag){
			 $('#addModal').modal("hide");
			 layer.alert(data.msg);
			 listui.refresh();
		 }else{
			 layer.msg(data.msg);
		 }
	});
});


//点击删除
$('.list-content-div table>tbody').delegate('tr>td>.delete-item', 'click', function() {
	var id = $(this).attr('data-id');
	var url =path + "/userManage/deleteItem/"+id+"?_t="+new Date().getTime();
	//询问框
	layer.confirm('确定删除该记录吗？', {
	  btn: ['确认','取消'] //按钮
	}, function(){
		//根据id 删除对应数据
		$.post(url,function(data){
			if(data.flag){
				layer.msg(data.msg);
				listui.refresh();
			}else{
				layer.msg(data.msg);
			}
		});
	});
});

//zTree属性设置
var setting = {
		callback: {
			//onCheck: zTreeOnCheck
		},
		check: {
			enable: true
		},
        view: {
        	showIcon: true,
			showLine: true,
			selectedMulti: false
        },
        data: {
        	key: {
    			name: "menuName"
    		},
            simpleData: {
                enable: true,
                idKey: "id",
    			pIdKey: "parentId",
    			rootPId: 0
            }
        }
    };


/*function zTreeOnCheck(event, treeId, treeNode) {
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = treeObj.getCheckedNodes();
	//console.log(nodes);
	
};*/


//保存按钮
$('#permissionSave').on('click',function(){
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = treeObj.getCheckedNodes();
//	var nodes = treeObj.getSelectedNodes();
	var arrMenuIds = new Array();
	var strMenuIds;
//	console.log($('#userId').val());
//	console.log(nodes);
	if(nodes.length!=0){
		for(var i = 0 ; i < nodes.length ; i++ ){
			arrMenuIds.push(nodes[i].id);
			strMenuIds = arrMenuIds.join(',');
		} 
//		console.log(arrMenuIds);
//		console.log(strMenuIds);
		var url = path+'/userManage/menuManage?_t='+new Date().getTime();
		var params = {'userId':$('#userId').val(),'menuIds':strMenuIds};
		$.post(url , params ,function(data){
			if(data.flag){
				$('#permissionModal').modal('hide');
				layer.alert(data.msg);
				listui.refresh();
				
			}
		});
	}else{
		layer.msg("请选择合适的菜单!");
	}
});

//点击权限分配
$('.list-content-div table>tbody').delegate('tr>td>.permission-item', 'click', function() {
	$('#userId').val($(this).attr('data-id'));
	//获取相应用户的菜单树
	$.post(path +'/userManage/menuTree/'+$('#userId').val()+'?_t='+new Date().getTime(),function(data){
		if(data.flag){
			var treeData=data.obj;
			$.fn.zTree.init($("#menuTree"),setting,treeData);
			var treeObj = $.fn.zTree.getZTreeObj("menuTree");
			treeObj.expandAll(true);
			//弹出对话框
			$('#permissionModal').modal({
				'show':true,
				'moveable':true
			});
		}else{
			layer.alert(data.obj);
		}
		
	});
	
});

if($('#userType').val()!=2){
	// 自定义表格操作按钮
	function handleOpRow(tr, obj) {
		
			tr.append($('<td/>').addClass('op').append($('<a/>', {
				"href" : "javascript:;",
				"data-id" : obj.id
			}).addClass('edit-item').text('编辑')).append($('<span/>').text('  |  ')).append(
					$('<a/>', {
						"href" : "javascript:;",
						"data-id" : obj.id
			}).addClass('delete-item').text('删除')).append($('<span/>').text('  |  ')).append(
					$('<a/>', {
						"href" : "javascript:;",
						"data-id" : obj.id
			}).addClass('permission-item').text('权限分配')));
	}
	
}else{
	
	// 自定义表格操作按钮
	function handleOpRow(tr, obj) {
		
			tr.append($('<td/>').addClass('op').append($('<a/>', {
				"href" : "javascript:;",
				"data-id" : obj.id
			}).addClass('edit-item').text('编辑')).append($('<span/>').text('  |  ')).append(
					$('<a/>', {
						"href" : "javascript:;",
						"data-id" : obj.id
			}).addClass('delete-item').text('删除')));
	}
	
}

// 自定义表格操作按钮
function handleOpRow(tr, obj) {
	
		tr.append($('<td/>').addClass('op').append($('<a/>', {
			"href" : "javascript:;",
			"data-id" : obj.id
		}).addClass('edit-item').text('编辑')).append($('<span/>').text('  |  ')).append(
				$('<a/>', {
					"href" : "javascript:;",
					"data-id" : obj.id
		}).addClass('delete-item').text('删除')).append($('<span/>').text('  |  ')).append(
				$('<a/>', {
					"href" : "javascript:;",
					"data-id" : obj.id
		}).addClass('permission-item').text('权限分配')));
	
		
		tr.append($('<td/>').addClass('op').append($('<a/>', {
			"href" : "javascript:;",
			"data-id" : obj.id
		}).addClass('edit-item').text('编辑')).append($('<span/>').text('  |  ')).append(
				$('<a/>', {
					"href" : "javascript:;",
					"data-id" : obj.id
				}).addClass('delete-item').text('删除')));
	
}